﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class ReturnsController : ControllerBase
	{
		public IReturnsBll _returnsBll;
		public ReturnsController(IReturnsBll returnsBll)
		{
			_returnsBll = returnsBll;
		}

		/// <summary>
		/// 获取列表信息
		/// </summary>
		/// <param name="page"></param>
		/// <param name="limit"></param>
		/// <returns></returns>
		[HttpGet]
		public IActionResult GetReturnsList(int page, int limit)
		{
			int count;

			//获取当前登录人信息
			UserInfo userInfo = this.GetSessionUserInfo();
			if (userInfo == null)
			{
				return new JsonResult(new
				{
					code = 0,
					msg = "成功",
					count = 0,
					data = new List<object>()
				});
			}
			object returns = _returnsBll.GetReturnsByPage(page, limit, out count);

			return new JsonResult(new
			{
				code = 0,
				msg = "成功",
				count = count,
				data = returns
			});
		}

		/// <summary>
		/// 获取状态下拉选
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public IActionResult GetStatusSelectOption()
		{
			CustomActionResult res = new CustomActionResult();
			var options = _returnsBll.GetStatusSelectOption();
			if (options != null)
			{
				res.Datas = options;
				res.IsSuccess = true;
				res.Status = 1;
				res.Msg = "成功";
			}
			return new JsonResult(res);
		}

		/// <summary>
		/// 揽收
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public IActionResult CommBuyoff([FromForm]string returnsId)
		{
			CustomActionResult res = new CustomActionResult();
			UserInfo userInfo = this.GetSessionUserInfo();
			bool isSuccess = _returnsBll.CommBuyoff(returnsId, userInfo.ID);
			if (isSuccess)
			{
				res.IsSuccess = true;
				res.Status = 1;
				res.Msg = "成功";
			}
			return new JsonResult(res);
		}

		/// <summary>
		/// 确认送达
		/// </summary>
		/// <param name="returnsId"></param>
		/// <returns></returns>
		[HttpPost]
		public IActionResult BtnCommDelivery([FromForm] string returnsId)
		{
			CustomActionResult res = new CustomActionResult();

			bool isSuccess = _returnsBll.BtnCommDelivery(returnsId);
			if (isSuccess)
			{
				res.IsSuccess = true;
				res.Status = 1;
				res.Msg = "成功";
			}
			return new JsonResult(res);
		}

	}
}
